package com.itheima.autoconfig;

import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.*;

@Data
@Component
@ConfigurationProperties(prefix = "tanhua.huanxin")
public class HuanXinTemplate {
    private String url;
    private String orgName;
    private String appName;
    private String clientId;
    private String clientSecret;

    public String getHuanXinUrl(){
        return this.url
                +this.orgName + "/"
                + this.appName;
    }

    private RestTemplate restTemplate=new RestTemplate();

    private String token;

    private long expire=0L;

    //获取token
    private String getToken(){
        long now = System.currentTimeMillis();
        if(now>expire){
            String url=this.url
                    +this.orgName + "/"
                    +this.appName + "token";
            HashMap<String, Object> param = new HashMap<>();
            param.put("grant_type","client_credentials");
            param.put("client_id",this.clientId);
            param.put("client_secret",this.clientSecret);

            ResponseEntity<Map> responseEntity = restTemplate.postForEntity(url, param, Map.class);
            Map<String,String> responseBody = responseEntity.getBody();
            token = responseBody.get("access_token");
            expire=System.currentTimeMillis()+24*60*60*1000;
        }
        return  token;
    }

    /*
    * 注册环信用户
    * */
    public void register(String userId){
        String url=getHuanXinUrl()+"/users";
        String token=getToken();
        //请求头信息
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Type","application/json");
        httpHeaders.add("Authorization","Bearer"+token);

        ArrayList<Map> huanXinUsers = new ArrayList<>();
        HashMap<Object, Object> huanxinMap = new HashMap<>();
        huanxinMap.put("username",userId);
        huanxinMap.put("password","123456");
        huanXinUsers.add(huanxinMap);

        try {
            HttpEntity httpEntity = new HttpEntity(JSON.toJSONString(huanXinUsers), httpHeaders);
            //发起请求
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /*
    * 添加好友
    * */
    public void addContacts(String userId,String friendId){
        String targetUrl=getHuanXinUrl()+"/users/"+userId+"/contacts/users/"+friendId;

        //请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type","application/json");
        headers.add("Authorization","Bearer"+getToken());
        try {
            HttpEntity<String> httpEntity = new HttpEntity<>(headers);
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(targetUrl, httpEntity, String.class);
        }catch (Exception e){

        }
    }

    /*
    * 发送消息
    * 目标对象id-陌生人id
    * 消息体msg-消息的json格式
    * */
    public void senMsg(String target,String msg){
        String targetUrl=getHuanXinUrl()+"/messagess";
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type","application/json");
        headers.add("Authorization","Bearer"+getToken());
        try {
            //请求头
            HashMap<String, Object> requestParam = new HashMap<>();
            requestParam.put("target_type","users");
            requestParam.put("target",Arrays.asList(target));

            HashMap<String, Object> msgParam = new HashMap<>();
            msgParam.put("msg",msg);
            msgParam.put("type","txt");

            requestParam.put("msg",msgParam);

            //表示消息发送者，无此字段会默认设置为 "from":"admin",有from字段但值为空船("")时请求失败
            //requestParam.put("from",null);
            HttpEntity<HashMap<String, Object>> httpEntity = new HttpEntity<>(requestParam, headers);
            ResponseEntity<String> responseEntity = restTemplate.postForEntity(targetUrl, httpEntity, String.class);

        }catch (Exception e){

        }
    }

    /*
    * 解除好友关系
    * */
    public void deleteContacts(String userId,String friendId){
        String targetUrl = getHuanXinUrl() + "/users/" + userId + "contacts/users/" + friendId;
        System.out.println(targetUrl);
        //请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type","application/json");
        headers.add("Authorization","Bearer"+getToken());
        try {
            HttpEntity<String> httpEntity = new HttpEntity<>(headers);
            restTemplate.exchange(targetUrl,HttpMethod.DELETE,httpEntity,String.class);
        }catch (Exception e){

        }
    }


}